﻿using System;
using System.Drawing;
using System.Windows.Forms;
using HuaCheng.Controller;
using HuaCheng.Model;
using HuaCheng.CommonClass;

namespace HuaCheng.UserControls
{
    /// <summary>
    /// 小图标通道
    /// </summary>
    public partial class UcChannelSmall : UserBase
    {
        //记录上一个channel状态
        private Channel lastChannel = new Channel();
        //通道数，用于控制显示
        public int chlCount = 0;
        //单位定制对象
        public new UnitOrder uOrder;
        #region 初始化
        public UcChannelSmall(string language)
        {
            InitializeComponent();
            SetResourceCulture(language);
            CheckForIllegalCrossThreadCalls = false;//允许线程之间的操作
        }
        /// <summary>
        /// 设置语言环境
        /// </summary>
        /// <param name="language"></param>
        public override void SetResourceCulture(string language)
        {
            ResourceCulture.SetCurrentCulture(language);
            //this.lblCapacity.Text = ResourceCulture.GetString("lblCapacityText");
        }
        /// <summary>
        /// 单位初始化
        /// </summary>
        public override void InitParam()
        {
            uOrder = UnitOrderControl.GetUnit();
        }
        /// <summary>
        /// 通道数据类
        /// </summary>
        public override Channel Channel
        {
            get { return channel; }
            set
            {
                if (value.ChlState == Channel.runningState.Finish || value.ChlState != lastChannel.ChlState || (chlCount % 100 == 0 && (value.ChlState == Channel.runningState.Charge || value.ChlState == Channel.runningState.DisCharge)))
                {
                    chlCount = 0;
                    try
                    {
                        channel = value;
                        //小的状态下页面显示数据
                        this.lblCHSmall.Text = value.Title == "" ? value.ChId : value.Title;
                        this.lblVoltageSmall.Text = uOrder.Voltage == "V" ? Convert.ToString(Math.Round(value.Voltage, 4)) : Convert.ToString(Math.Round(value.Voltage * 1000, 4));//电压
                        this.lblCurrentSmall.Text = uOrder.Current == "A" ? Convert.ToString(Math.Round(value.Current, 4)) : Convert.ToString(Math.Round(value.Current * 1000, 4));//电流
                        this.lblRunStat.Text = Convert.ToString(value.ChlStepName);
                        this.BackColor = value.ucColor;//设置通道颜色
                        if (value.LockedStat)//锁定
                        {
                            this.lblRunStat.Text = "";
                            this.lblCurrentSmall.Text = "";
                            this.lblLocked.Text = ResourceCulture.GetString("Locked");
                        }
                        else
                        {
                            this.lblLocked.Text = "";
                        }
                        switch (value.ChlState)
                        {
                            case Channel.runningState.Finish://完成
                                this.lblRunStat.ForeColor = Color.Green;
                                DefaultParam();
                                break;
                            case Channel.runningState.Charge://充电
                                this.lblRunStat.ForeColor = Color.Blue;
                                break;
                            case Channel.runningState.DisCharge://放电
                                this.lblRunStat.ForeColor = Color.Blue;
                                break;
                            case Channel.runningState.Shelve://搁置
                                this.lblRunStat.ForeColor = Color.Blue;
                                break;
                            case Channel.runningState.Protect://保护
                                break;
                            case Channel.runningState.Stop://停止
                                this.lblRunStat.ForeColor = Color.Blue;
                                DefaultParam();
                                break;
                            case Channel.runningState.Pause://暂停
                                break;

                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + ex.StackTrace);
                    }
                }
                chlCount++;
                lastChannel.ChlState = value.ChlState;
            }
        }
        /// <summary>
        /// 点击子控件时触发父控件点击事件，避免父控件点击事件被覆盖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public new void lbl_MouseClick(object sender, MouseEventArgs e)
        {
            base.OnMouseClick(e);
        }
        /// <summary>
        /// 点击子控件时触发父控件点击事件，避免父控件点击事件被覆盖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public new void lbl_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(e);
        }
        /// <summary>
        /// 默认显示内容
        /// </summary>
        public void DefaultParam()
        {
            this.lblCurrentSmall.Text = "";
        }
        #endregion
    }
}
